
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

MainScene.RESOURCE_FILENAME = "MainScene.csb"

function MainScene:onCreate()

    local writablePath = cc.FileUtils:getInstance():getWritablePath()
    local storagePath = writablePath .. "new_version"
    --将下载目录的src和res作为优先级最高的搜索目录，这样才能保证下载的能覆盖原来的代码
    cc.FileUtils:getInstance():addSearchPath(storagePath.."/src/",true)
    cc.FileUtils:getInstance():addSearchPath(storagePath.."/res/",true)

    -- 创建AssetsManagerEx对象
    local assetsManagerEx = cc.AssetsManagerEx:create("src/version/project.manifest", storagePath)
    assetsManagerEx:retain()

    local labels1 = cc.Label:createWithTTF("", "DengXian.ttf", 32);
    labels1:setColor(cc.c3b(255,0,255));
    labels1:setPosition(400,400);
    self:addChild(labels1);

    -- 设置下载消息listener
    local function handleAssetsManagerEx(event)
        if (cc.EventAssetsManagerEx.EventCode.ALREADY_UP_TO_DATE == event:getEventCode()) then
            local labels = cc.Label:createWithTTF("1已经是最新版本了，进入游戏主界面", "DengXian.ttf", 32);
            labels:setColor(cc.c3b(255,0,255));
            labels:setPosition(400,500);
            self:addChild(labels);
 
            -- app:enterScene("GameScene")
        end

        if (cc.EventAssetsManagerEx.EventCode.NEW_VERSION_FOUND == event:getEventCode()) then
            local labels = cc.Label:createWithTTF("2发现新版本，开始升级", "DengXian.ttf", 32);
            labels:setColor(cc.c3b(255,0,255));
            labels:setPosition(400,450);
            self:addChild(labels);
        end


        if (cc.EventAssetsManagerEx.EventCode.UPDATE_PROGRESSION == event:getEventCode()) then
            labels1:setString("更新进度=" .. event:getPercent());
            
        end


        if (cc.EventAssetsManagerEx.EventCode.UPDATE_FINISHED == event:getEventCode()) then
            local labels = cc.Label:createWithTTF("更新完毕，重新启动", "DengXian.ttf", 32);
            labels:setColor(cc.c3b(255,0,255));
            labels:setPosition(400,350);
            self:addChild(labels);
            app:run()
        end

        if (cc.EventAssetsManagerEx.EventCode.ERROR_NO_LOCAL_MANIFEST == event:getEventCode()) then
            local labels = cc.Label:createWithTTF("发生错误:本地找不到manifest文件", "DengXian.ttf", 32);
            labels:setColor(cc.c3b(255,0,255));
            labels:setPosition(400,300);
            self:addChild(labels);
        end

        if (cc.EventAssetsManagerEx.EventCode.ERROR_DOWNLOAD_MANIFEST == event:getEventCode()) then
            local labels = cc.Label:createWithTTF("发生错误:下载manifest文件失败", "DengXian.ttf", 32);
            labels:setColor(cc.c3b(255,0,255));
            labels:setPosition(400,200);
            self:addChild(labels);
        end

        if (cc.EventAssetsManagerEx.EventCode.ERROR_PARSE_MANIFEST == event:getEventCode()) then
            local labels = cc.Label:createWithTTF("发生错误:解析manifest文件失败", "DengXian.ttf", 32);
            labels:setColor(cc.c3b(255,0,255));
            labels:setPosition(400,150);
            self:addChild(labels);
        end

        if (cc.EventAssetsManagerEx.EventCode.ERROR_UPDATING == event:getEventCode()) then
            local labels = cc.Label:createWithTTF("发生错误:更新失败", "DengXian.ttf", 32);
            labels:setColor(cc.c3b(255,0,255));
            labels:setPosition(400,100);
            self:addChild(labels);
        end
    end


    local dispatcher = cc.Director:getInstance():getEventDispatcher()
    local eventListenerAssetsManagerEx = cc.EventListenerAssetsManagerEx:create(assetsManagerEx, handleAssetsManagerEx)
    dispatcher:addEventListenerWithFixedPriority(eventListenerAssetsManagerEx, 1)


    -- 检查版本并升级
    assetsManagerEx:update()
end

return MainScene
